package com.atxiao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ServerMakeFile {
    public static File mkTemporaryDirs() throws IOException {

//        String filePath = "temporaryDir/file/temp.txt";

//        String filePath = "temporaryDir/home/demo/temp.txt";


        String filePath="/home/demo/demo.txt";
        File file = new File(filePath);
        if (!file .getParentFile().exists()) {
            file .getParentFile().mkdirs();
        }
        if(!file .exists()) {
            file .createNewFile();
        }
        return file;
    }

    public static Boolean inputStreamToFile(String context, File file) throws IOException {

        Boolean flag=false;
        if(!file .exists()) {
            file .createNewFile();
        }
        file.setExecutable(true);
        file.setReadable(true);
        file.setWritable(true);
        try {
            OutputStream os = new FileOutputStream(file);
            os.write(context.getBytes("GBK"));
            if(os!=null){
                os.close();
            }
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();

            return flag;
        }
        return flag;
    }



    public static void main(String[] args) throws IOException {
        File file = mkTemporaryDirs();
        System.out.println(file.getAbsolutePath());
        Boolean flag = inputStreamToFile("dddddddd", file);
        System.out.println(flag);

    }
}
